import { createFileRoute } from '@tanstack/react-router'
import Dicts from '@/features/dicts'
import { dictKeys, dicts, dictsTableTree, dictsSelectTree } from '@/api/dicts'
import { DictItem } from '@/features/dicts/data/schema'
import { TreeItem, ListItem } from '@/components/tree-select/schema'

export const Route = createFileRoute('/_authenticated/dicts/')({
  component: Dicts,
  loader: async ({ context: { queryClient } }): Promise<{ dictList: DictItem[]; dictsTree: TreeItem[]; dictsSelect: ListItem[] }> => {
    const res = await queryClient.fetchQuery({
      queryKey: dictKeys.all(),
      queryFn: () => dicts({ limit: 100000 })
    })

    const dictsTreeRes = await queryClient.fetchQuery({
      queryKey: dictKeys.dictsTableTree(),
      queryFn: dictsTableTree
    })

    const dictsSelectRes = await queryClient.fetchQuery({
      queryKey: dictKeys.dictsSelectTree(),
      queryFn: dictsSelectTree
    })

    return { dictList: res.data.data, dictsTree: dictsTreeRes.data.data, dictsSelect: dictsSelectRes.data.data }
  }
})


